<template>
  <div class="shelf-search-wrapper" :class="{'search-top': ifInputClicked, 'hide-shadow': ifHideShadow}">
    <div class="shelf-search" :class="{'search-top': ifInputClicked}">
      <div class="search-wrapper">
        <div class="icon-search-wrapper">
          <span class="icon-search icon" />
        </div>
        <div class="search-input-wrapper">
          <input :placeholder="$t('shelf.search')" class="search-input" type="text" @click="onSearchClick" v-model.trim="searchText"/>
        </div>
        <div class="icon-clear-wrapper" v-show="searchText" @click="onSearchClear">
          <span class="icon-close-circle-fill icon" />
        </div>
      </div>
      <div class="icon-locale-wrapper" v-if="!ifInputClicked" @click="handleSwitchLocale">
        <span class="icon-cn icon" v-if="lang === 'cn'"/>
        <span class="icon-en icon" v-else/>
      </div>
      <div class="cancel-btn-wrapper" v-if="ifInputClicked" @click="onCancelClick">
        <span class="cancel-text">{{$t('shelf.cancel')}}</span>
      </div>
    </div>
    <transition name="shelf-tab-slide-up">
      <div class="shelf-search-tab-wrapper" v-if="ifInputClicked">
        <div class="shelf-search-tab-item" v-for="item in tabs" :key="item.id" @click="onTabClicked(item.id)">
          <span class="shelf-search-tab-text" :class="{'is-selected': item.id === selectedTab}">{{item.text}}</span>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
  import {switchLocale} from "../../utils/tools";
  import {storeShelfMixin} from "../../mixins/storeShelfMixin";

  export default {
    name: "ShelfSearch",
    mixins: [storeShelfMixin],
    data() {
      return {
        ifInputClicked: false,
        ifHideShadow: true,
        searchText: '',
        selectedTab: 1
      }
    },
    computed: {
      lang() {
        return this.$i18n.locale
      },
      tabs() {
        return [
          {
            id: 1,
            text: this.$t('shelf.default'),
            selected: true
          },
          {
            id: 2,
            text: this.$t('shelf.progress'),
            selected: false
          },
          {
            id: 3,
            text: this.$t('shelf.purchase'),
            selected: false
          }
        ]
      }
    },
    methods: {
      onSearchClick() {
        this.ifInputClicked = true
        this.setShelfTitleVisible(false)
      },
      onCancelClick() {
        this.ifInputClicked = false
        this.setShelfTitleVisible(true)
      },
      handleSwitchLocale() {
        switchLocale(this)
      },
      onSearchClear() {
        this.searchText = ''
      },
      onTabClicked(id) {
        this.selectedTab = id
      }
    },
    watch: {
      offsetY(val) {
        if (val > 0 && this.ifInputClicked) {
          this.ifHideShadow = false
        } else {
          this.ifHideShadow = true
        }
      }
    }
  }
</script>

<style scoped lang="scss">
  @import "../../assets/styles/global";
  .shelf-search-wrapper {
    position: relative;
    z-index: 105;
    width: 100%;
    height: px2rem(94);
    font-size: px2rem(16);
    background: #ffffff;
    box-shadow: 0 px2rem(2) px2rem(2) 0 rgba(0,0,0,.1);
    &.hide-shadow {
      box-shadow: none
    }
    &.search-top {
      position: fixed;
      top: 0;
      left: 0;
    }
    .shelf-search {
      position: absolute;
      top: px2rem(42);
      left: 0;
      z-index: 105;
      display: flex;
      width: 100%;
      height: px2rem(52);
      transition: top $animationTime linear;
      &.search-top {
        top: 0
      }
      .search-wrapper {
        flex: 1;
        display: flex;
        margin: px2rem(8) 0 px2rem(8) px2rem(10);
        border: px2rem(1) solid #ccc;
        border-radius: px2rem(3);
        .icon-search-wrapper {
          flex: 0 0 px2rem(22);
          @include right();
          .icon-search {
            font-size: px2rem(12);
          }
        }
        .search-input-wrapper {
          flex: 1;
          @include center();
          padding: 0 px2rem(10);
          box-sizing: border-box;
          .search-input {
            width: 100%;
            font-size: px2rem(14);
            border: none;
            color: #333;
            &:focus {
              outline: none;
            }
            &::-webkit-input-placeholder {
              font-size: px2rem(14);
              color: #ccc;
            }
          }
        }
        .icon-clear-wrapper {
          flex: 0 0 px2rem(24);
          @include left();
          .icon-close-circle-fill {
            font-size: px2rem(14);
            color: #ccc;
          }
        }
      }
      .icon-locale-wrapper {
        flex: 0 0 px2rem(55);
        @include center();
        .icon-cn,.icon-en {
          font-size: px2rem(22);
          color: #666;
        }
      }
      .cancel-btn-wrapper {
        flex: 0 0 px2rem(55);
        @include center();
        .cancel-text {
          font-size: px2rem(14);
          color: $color-blue;
        }
      }
    }
    .shelf-search-tab-wrapper {
      position: absolute;
      top: px2rem(52);
      left: 0;
      z-index: 100;
      display: flex;
      width: 100%;
      height: px2rem(42);
      .shelf-search-tab-item {
        flex: 1;
        @include center;
        .shelf-search-tab-text {
          font-size: px2rem(12);
          color: #999;
          &.is-selected {
            color: $color-blue;
          }
        }
      }
    }
  }
</style>
